using System.Linq;
using Olemas.Models;

namespace Olemas.DataModels
{
    public class TestModel
    {

        /// <summary>
        /// Gets the number of tests with the given faculty id and return all tests if faculty id is "0".
        /// </summary>
        /// <param name="facId">The Id of the faculty of the tests displayed.</param>
        public static int GetNumberOfTests(int facId)
        {
            var db = new OlemasDatabaseEntities();
            var total = (from test in db.Tests
                         where (facId == 0 || test.Course.Faculty_Id == facId)
                         select test).Count();
            return total;
        }

        /// <summary>
        /// Gets the tests with the given faculty id.
        /// </summary>
        /// <param name="facId">The Id of the faculty of the tests displayed.</param>
        /// <param name="page">The current page</param>
        /// <param name="pageSize">The amount of tests to display on one page.</param>
        /// <param name="ordering">What to order the books by.</param>
        public static IQueryable<Test> GetTests(int facId, int page, int pageSize, string ordering)
        {
            var db = new OlemasDatabaseEntities();

            var tests = db.Tests.Where(t => facId == 0 || t.Course.Faculty_Id == facId);

            switch (ordering)
            {
                case "NewestFirst":
                    tests = tests.OrderByDescending(t => t.DateAdded);
                    break;

                case "OldestFirst":
                    tests = tests.OrderBy(t => t.DateAdded);
                    break;

                case "Course":
                    tests = tests.OrderBy(t => t.Course.Name);
                    break;

                case "Name":
                    tests = tests.OrderBy(t => t.Name);
                    break;

                default:
                    tests = tests.OrderByDescending(t => t.DateAdded);
                    break;
            }
            return tests.Skip(pageSize * (page - 1)).Take(pageSize);

        }
        /// <summary>
        /// Gets the test with the given id.
        /// </summary>
        /// <param name="id">The Id of the test.</param>
        public static Test GetTestById(int id)
        {
            var db = new OlemasDatabaseEntities();
            var test = db.Tests.Find(id);
            return test;
        }
    }
}